--
-- Author: shunguo.chen
-- Date: 2017-11-27 11:42:45
--
-- MountActiveRender 激活的坐骑

local MountRender = import(".MountRender")

local ClassRef = app.mvc.createRenderClass(...)

--data = Mount
function ClassRef.createWithData(data, node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.MountActiveNode1))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end       
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end 

function ClassRef:init(data)
	self.super.init(self, data)

	self:setContentSize(self._ui.Panel:getContentSize())
	
	self._render = MountRender.create(self._ui["Panel/Panel_icon/icon"])
    self._render:setClickEnable(false)
    
    self._selectNode = self._ui["Panel/Panel_icon/mount_effect_1_1"]
    self._star = self._ui["Panel/Panel_icon/star"]
    self._iconPanel = self._ui["Panel/Panel_icon"]
    self._btnAdd = self._ui["Panel/btn_add"]
    self._isLock = true

    self._iconPanel:setTouchEnabled(false)
	display.uiAddClick(self._ui.Panel, function(sender, eventType)
	    if self._data and not self:isSelected() then
            self:setSelected(not self:isSelected())
            executeFunc(self._onClick, self)
        end
    end)
    
    self._btnAdd:addClickEventListener(function()
        local conf = GD:queryItemById(Constant.Mount_ExtendActiveSlot)
        if not conf then
            return
        end

        local myItem = UD:getItemByTid( Constant.Mount_ExtendActiveSlot )
        local itemName = L(conf.name)
        if not myItem then
            display.pushToast(string.format(L("auction_tips9"), itemName))
            return
        end
        local confirmDialog = display.newConfirmDialog(string.format(L("MountExtentActiveSlot"), itemName),
            function()
                app:sendMsg("NewBagModel", "reqUseItem", myItem.uid, myItem.tid, 1)
            end)
        cc.Director:getInstance():getRunningScene():addChild(confirmDialog, 1000)
    end)

	self:refresh()
end

function ClassRef:setLock(trueOrFalse)
    self._isLock = trueOrFalse
    self:refresh()
end

function ClassRef:refresh()
    -- self:setVisible(self._data ~= nil)
    if self._isLock then
        self._iconPanel:setVisible(false)
        self._btnAdd:setVisible(true)
    else
        self._btnAdd:setVisible(false)
        self._iconPanel:setVisible(self._data ~= nil)
        if self._data then
            self._render:setData(self._data)
            self._render:hideStarPanel()
            local star = self._data:getStar()
            self._star:loadTexture(string.format(Res.MountStarPath, star))
        -- else
        --     self._render:setData()
        end
    end
end

function ClassRef:isSelected()
    return self._selectNode:isVisible()
end
function ClassRef:setSelected(b)
    self._selectNode:setVisible(b)
end

function ClassRef:setClickEnable(b)
	self._ui.Panel:setTouchEnabled(b)
end

function ClassRef:setClickHandler(cb)
    self._onClick = cb
end

function ClassRef:setBtnClick(cb)
	self._onClick = cb
end

return ClassRef